package cn.com.connext.scrm.utils.wechat;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class JsApiTicketUtils {


    private static RestTemplate restTemplate = new RestTemplate();

    private static Logger log = LoggerFactory.getLogger(JsApiTicketUtils.class);
    private static Map<String, String> jsApiTicketMap = new HashMap<>();
    private final static String JSAPI_TICKET = "jsapi";//jsapi_ticket
    private final static String WX_CARD_JSAPI_TICKET = "wx_card";//微信卡券jsapi_ticket

    /**
     * 获取jsapi_ticket
     */
    public static Map<String, String> getJsApiTicket(String accessToken) {
        String time = jsApiTicketMap.get("jsapi_ticket_time");
        String jsApiTicket = jsApiTicketMap.get("jsapi_ticket");
        Long nowDate = new Date().getTime();
        if (jsApiTicket != null && time != null && nowDate - Long.parseLong(time) < (1.5 * 60 * 60 * 1000)) {
            log.info("jsApiTicket存在，且没有超时，返回jsApiTicketMap");
            return jsApiTicketMap;
        }
        synchronized (JsApiTicketUtils.class) {
            if (jsApiTicket != null && time != null && nowDate - Long.parseLong(time) < (1.5 * 60 * 60 * 1000)) {
                log.info("jsApiTicket存在，且没有超时，返回jsApiTicketMap");
                return jsApiTicketMap;
            }
            log.info("jsApiTicket超时，或者不存在，重新获取");
            try {
                //根据accessToken获取jsapiTicket
                String jsapiTicket = WeiXinUtils.getJsApiTicket(accessToken, JSAPI_TICKET);
                log.info("jsapi_ticket_time:" + nowDate);
                log.info("jsapi_ticket:" + jsapiTicket);
                jsApiTicketMap.put("jsapi_ticket_time", nowDate + "");
                jsApiTicketMap.put("jsapi_ticket", jsapiTicket);
            } catch (Exception e) {
                log.error("微信服务器发生错误", e);
            }
            return jsApiTicketMap;
        }
    }

    /**
     * 获取jsapi_ticket(微信卡券)
     */
    public static Map<String, String> getWxCardJsApiTicket(String accessToken) {
        String time = jsApiTicketMap.get("wx_card_jsapi_ticket_time");
        String jsApiTicket = jsApiTicketMap.get("wx_card_jsapi_ticket");
        Long nowDate = new Date().getTime();
        if (jsApiTicket != null && time != null && nowDate - Long.parseLong(time) < (1.5 * 60 * 60 * 1000)) {
            log.info("jsApiTicket存在，且没有超时，返回jsApiTicketMap");
            return jsApiTicketMap;
        }
        synchronized (JsApiTicketUtils.class) {
            if (jsApiTicket != null && time != null && nowDate - Long.parseLong(time) < (1.5 * 60 * 60 * 1000)) {
                log.info("jsApiTicket存在，且没有超时，返回jsApiTicketMap");
                return jsApiTicketMap;
            }
            log.info("jsApiTicket超时，或者不存在，重新获取");
            try {
                //根据accessToken获取jsapiTicket
                String jsapiTicket = WeiXinUtils.getJsApiTicket(accessToken, WX_CARD_JSAPI_TICKET);
                log.info("wx_card_jsapi_ticket_time={}" + nowDate);
                log.info("wx_card_jsapi_ticket={}" + jsapiTicket);
                jsApiTicketMap.put("wx_card_jsapi_ticket_time", nowDate + "");
                jsApiTicketMap.put("wx_card_jsapi_ticket", jsapiTicket);
                log.info("jsApiTicketMap={}", JSON.toJSONString(jsApiTicketMap));
            } catch (Exception e) {
                log.error("微信服务器发生错误", e);
            }
            return jsApiTicketMap;
        }

    }

}
